package com.yzjs.localhost.service.hrmFamilyMembers.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmEducationInfo.HrmEducationInfoEntity;
import com.yzjs.localhost.entity.hrmFamilyMembers.HrmFamilyMembersEntity;
import com.yzjs.localhost.dao.hrmFamilyMembers.HrmFamilyMembersMapper;
import com.yzjs.localhost.entity.hrmProfessionalQualification.HrmProfessionalQualificationEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmEducationInfo.impl.HrmEducationInfoServiceImpl;
import com.yzjs.localhost.service.hrmFamilyMembers.HrmFamilyMembersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("hrmFamilyMembersService")
public class HrmFamilyMembersServiceImpl extends BaseServiceImpl<HrmFamilyMembersEntity, HrmFamilyMembersMapper> implements HrmFamilyMembersService {
    @Autowired
    AnalysisService analysisService;

    @Autowired
    HrmFamilyMembersService hrmFamilyMembersService;

    @Override
    public ComparisonResultDTO hrmFamilyMembersCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        Map<String, Object> afterParseMap = JsonUtil.json2map(after.getInfo());
        Map<String, Object> differenceMap = new HashMap<>();
        ComparisonResultDTO resultDTO = null;

        if (before.getOperatorCode().contains("family:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 证件类型,学历,亲属称谓,政治面貌,联系电话,创建人,用户ID,证件号码,姓名,出生日期
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "famCredentials,famEducation,famRelation,famPoliticalOutlook,famTelphone,createBy,staffUserId,famNums,famName,famBirthDate");

        } else if (before.getOperatorCode().contains("family:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            //比对 证件类型,部门名称,学历,主键ID,fullName,亲属称谓(系统字典表),更新时间,staffNum,删除标志,政治面貌,联系电话,创建人,用户ID,更新人,证件号码,姓名,出生日期,创建时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "famCredentials,deptName,famEducation,famId,fullName,famRelation,updateTime,staffNum,delFlag,famPoliticalOutlook,famTelphone,createBy,staffUserId,updateBy,famNums,famName,famBirthDate,createDate");

        } else if (before.getOperatorCode().contains("family:remove")) {
            beforeParse.put("delFlag", "2");
            // 比对家庭成员记录主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParseMap, "famIds,delFlag");
            Object famId = afterParseMap.get("famIds");
            if (famId == null) {
                resultDTO.setAbstractName("主键ID不存在");
                resultDTO.setWarningInfo("主键ID不存在");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmFamilyMembersEntity familyMembersEntity = new HrmFamilyMembersEntity();
            familyMembersEntity.setFamId(Long.valueOf((String) famId));
            familyMembersEntity.setDelFlag((String) afterParseMap.get("delFlag"));
            familyMembersEntity.setDataIdentification(after.getDataIdentification());
            this.insert(familyMembersEntity);
            return resultDTO;
        }
        HrmFamilyMembersEntity hrmFamilyMembersEntity = JSONObject.parseObject(afterInfo, HrmFamilyMembersEntity.class);
        hrmFamilyMembersEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmFamilyMembersEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmFamilyMembersEntity);
        return resultDTO;
    }
}